package me.firstapp.api.shop;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;

import me.firstapp.api.base.BaseApi;
import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.utils.ResponseUtils;
import me.firstapp.module.shop.Address;
import me.firstapp.service.shop.AddressService;

@Controller
@RequestMapping(value = "/address")
@Api(value = "/address", description = "收货地址相关接口")
public class AddressApi extends BaseApi {

	@Autowired
	private AddressService addressService;

	@RequestMapping(value = "/addUpdateAddress.do", method = RequestMethod.POST)
	@ApiOperation(value = "新增/修改收货地址", notes = "新增/修改收货地址")
	public void addUpdateAddress(Long memberId, String name, String mobile, String province, String city,
			String district, String detail, HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Address> result = new SingleObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			Address address = addressService.addUpdateAddress(memberId, name, mobile, province, city, district, detail,
					apiClientId);
			result.setObject(address);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "/findAddressByMemberId.do", method = RequestMethod.GET)
	@ApiOperation(value = "查询用户收货地址", notes = "查询用户收货地址")
	public void findAddressByMemberId(Long memberId, HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Address> result = new SingleObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			Address address = addressService.findAddressByMemberId(memberId, apiClientId);
			result.setObject(address);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

}
